﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">Unit testing</title>
  <keywords>
    <keyword translate="true">Unit testing</keyword>
  </keywords>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Unit testing</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">If you are wondering why you should even write unit tests, you should also wonder why you aren’t still living in a cage and writing stuff on the walls (don&apos;t feel offended, continue reading...). Writing unit tests makes sure that you don’t break existing functionality in an application when making changes. This lowers the cost of QA (since you don’t need a technical tester executing regression tests all the time). I don’t say that a tester isn’t needed; my opinion is that at least someone else besides the developer should take a human look at the software before it is even released. If you are still not convinced why you should write unit tests, please go back to your cave and stop reading this article for the sake of your own pleasure.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">This documentation does not cover the basics of unit testing. It assumes that you already know what unit tests are, and how to write them. This documentation is specifically written to explain how view models of the MVVM pattern can be unit tested, especially with the use of Catel.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Testing commands</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Thanks to commands (which implement the </text><link displaytype="text" defaultstyle="true" type="weblink" href="http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.aspx" target="_blank" styleclass="Normal" translate="true">ICommand</link><text styleclass="Normal" translate="true"> interface), testing view models and UI logic has never been so easy. Now commands can be invoked programmatically without having to automate the UI; it is very simple to reproduce a button click during a unit test.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">When testing commands, it is very important to test the state as well. The command implementation of Catel has a </text><link displaytype="text" defaultstyle="true" type="topiclink" href="M_Catel_MVVM_Command`2_CanExecute" styleclass="Normal" translate="true">CanExecute</link><text styleclass="Normal" translate="true"> and an </text><link displaytype="text" defaultstyle="true" type="topiclink" href="M_Catel_MVVM_Command`2_Execute" styleclass="Normal" translate="true">Execute </link><text styleclass="Normal" translate="true">method which can be invoked manually. Therefore, it is very easy to test a command. The code below shows a test that checks whether the </text><text styleclass="Normal" style="font-style:italic;" translate="true">Remove </text><text styleclass="Normal" translate="true">command can be executed. At first, the command cannot be executed because there is no selected person. After the selected person is set, the command should be able to execute:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">Assert.IsFalse(mainWindowViewModel.Remove.CanExecute(</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">null</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">));</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">mainWindowViewModel.SelectedPerson = mainWindowViewModel.PersonCollection[0];</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">Assert.IsTrue(mainWindowViewModel.Remove.CanExecute(</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">null</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">));</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">To execute a command in code, one should use the following code:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">mainWindowViewModel.Remove.Execute(</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">null</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">);</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Testing services</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">For more information about unit testing services, see the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="GS_UnitTesting_Unit_testing_services" styleclass="Normal" translate="true">unit testing services documentation</link><text styleclass="Normal" translate="true">.</text></para>
  </body>
</topic>
